import { request } from '@/utils/request'
import type { SysPermission } from '@/types/permission'
import type { PageQuery } from '@/types/api'

// 获取权限列表
export function getPermissionList(params?: Record<string, any>) {
  return request<SysPermission[]>({
    url: '/permission/list',
    method: 'get',
    params
  })
}

// 分页获取权限列表
export function getPermissionPage(params: PageQuery & Record<string, any>) {
  return request<{
    rows: SysPermission[]
    total: number
  }>({
    url: '/permission/page',
    method: 'get',
    params
  })
}

// 获取权限详情
export function getPermissionDetail(permissionId: number) {
  return request<SysPermission>({
    url: `/permission/${permissionId}`,
    method: 'get'
  })
}

// 新增权限
export function addPermission(data: Partial<SysPermission>) {
  return request({
    url: '/permission',
    method: 'post',
    data
  })
}

// 修改权限
export function updatePermission(data: Partial<SysPermission>) {
  return request({
    url: '/permission',
    method: 'put',
    data
  })
}

// 删除权限
export function deletePermission(permissionId: number) {
  return request({
    url: `/permission/${permissionId}`,
    method: 'delete'
  })
}

// 获取用户权限
export function getUserPermission(userId: number) {
  return request<SysPermission[]>({
    url: `/permission/user/${userId}`,
    method: 'get'
  })
}

// 获取角色权限树
export function getRolePermissionTree(roleId: number) {
  return request<{
    menus: any[]
    checkedKeys: number[]
  }>({
    url: `/permission/rolePermissionTreeSelect/${roleId}`,
    method: 'get'
  })
}
